package com.itydl.sweepview1.adapter;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.itydl.sweepview1.R;
import com.itydl.sweepview1.view.SweepView;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ydl
 * @version
 */

public class MyAdapter extends BaseAdapter {

    //存放打开过的item（SweepView）
    List<SweepView> mSweepViews = new ArrayList<>();
    List<String> mDatas = new ArrayList<>();

    public MyAdapter(List<String> mDatas){
        this.mDatas = mDatas;
    }

    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if(convertView == null){
            holder = new ViewHolder();
            convertView = View.inflate(parent.getContext(),R.layout.item_listview,null);
            holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
            holder.tv_delete = (TextView) convertView.findViewById(R.id.tv_delete);
            holder.tv_Stick = (TextView) convertView.findViewById(R.id.tv_Stick);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }

        SweepView sweepView = (SweepView) convertView;
        sweepView.setOnSweepViewListener(new MyOnDragSweepViewListener());
        holder.tv_delete.setOnClickListener(new MyOnclickListener(position));
        holder.tv_Stick.setOnClickListener(new MyOnclickListener(position));

        holder.tv_name.setText(mDatas.get(position));

        return convertView;
    }

    class MyOnclickListener implements View.OnClickListener{

        int position;

        public MyOnclickListener(int position){
            this.position = position;
        }

        @Override
        public void onClick(View v) {
            if(v.getId() == R.id.tv_delete){
                mDatas.remove(position);
                notifyDataSetChanged();
            }else if(v.getId() == R.id.tv_Stick){
                String currentData = mDatas.get(position);
                mDatas.remove(position);
                mDatas.add(0,currentData);
                notifyDataSetChanged();
            }
        }
    }

    class MyOnDragSweepViewListener implements SweepView.OnDragSweepViewListener{

        @Override
        public void open(SweepView sweepView) {
            mSweepViews.add(sweepView);
        }

        @Override
        public void close(SweepView sweepView) {
            mSweepViews.remove(sweepView);
        }

        @Override
        public void onStartOpen(SweepView sweepView) {
            //关闭所有的SweepView
            cloasAllSweepView();
        }

        @Override
        public void onStartClose(SweepView sweepView) {
            cloasAllSweepView();
        }
    }

    public void cloasAllSweepView() {
        for (int i = 0; i < mSweepViews.size(); i++) {
            mSweepViews.get(i).close();
        }
    }

    class ViewHolder{
        TextView tv_Stick,tv_delete,iv_header,tv_name;
    }
}
